package com.huiminpay.paymentagent.message;

import com.alibaba.fastjson.JSONObject;
import com.huiminpay.paymentagent.api.PayChannelAgentService;
import com.huiminpay.paymentagent.api.conf.AliConfigParam;
import com.huiminpay.paymentagent.api.dto.PaymentResponseDTO;
import com.huiminpay.paymentagent.api.dto.TradeStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "TP_PAYMENT_ORDER", consumerGroup = "CID_PAYMENT_CONSUMER")
public class PayConsumer implements RocketMQListener<MessageExt> {
     @Autowired
     PayProducer payProducer;
      @Reference
      private PayChannelAgentService payService;

    @Override
    public void onMessage(MessageExt messageExt) {
        int times = messageExt.getReconsumeTimes();
        if (times>0){
            System.out.println("第"+times+"重试");
        }
        if (times>2){
            System.out.println("消费处理完成");
            return;
        }

        byte[] bys = messageExt.getBody();
        String msg = new String(bys);
        //转对象
        PaymentResponseDTO msgDTO = JSONObject.parseObject(msg, PaymentResponseDTO.class);
        //公共参数
        AliConfigParam aliConfigParam =JSONObject.parseObject(msgDTO.getContent().toString(),AliConfigParam.class);
        //商户订单号
        String outTradeNo = msgDTO.getOutTradeNo();    
        //支付渠道
        String payChannel = msgDTO.getMsg();
        PaymentResponseDTO<?> result=new PaymentResponseDTO<>();
        if("ALIPAY_WAP".equals(payChannel)){
            //调支付宝接口
                result = payService.queryPayOrderByCli(aliConfigParam, outTradeNo);
        }else if ("WX_JSAPI".equals(payChannel)) {
            //调微信查询接口
            //result= payService.queryPayOrderByCli(aliConfigParam, outTradeNo);
        }
        //未查到支付结果,进行重试
        if (result.getTradeState().equals(TradeStatus.UNKNOWN)||
                result.getTradeState().equals(TradeStatus.USERPAYING)){
            throw new RuntimeException("交易结果未知,等待消费重试");
        }
       //发送结果结果
        payProducer.payResultNotice(result);


    }
}
